package com.bjpowernode.boss.common.client;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author yu
 * @date 2024/7/7  17:16
 */

@Component
public class RedisClient {
    // String的模板类
    private StringRedisTemplate stringRedisTemplate ;
    private HashOperations<String, Object, Object> hashOperations ;

    public RedisClient(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
        hashOperations = stringRedisTemplate.opsForHash() ;
    }

    /**
     * 定义操作方法
     * @param key
     * @param data
     * @param timeout 过期时间
     * @param timeUnit 时间单位
     * @return 写入Redis数据库是否成功
     */
    public Boolean setHashMap(String key, Map<String, String> data, int timeout, TimeUnit timeUnit) {
        hashOperations.putAll(key, data);
        return stringRedisTemplate.expire(key, timeout, timeUnit) ;
    }
}
